<template>
  <section class="grant-detail">
    <section class="detail-main">
      <section class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="search.userRealname" placeholder="输入姓名搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>

      <section class="main-buttons">
        <section class="main-buttons-title">
          <span>福利明细</span>
        </section>
        <section class="main-buttons-btns">
          <el-button style="margin-left: 10px;" @click="handleExport()">批量导出</el-button>
          <el-button style="padding: 10px 30px;" @click="handleCancel()">返回</el-button>
        </section>
      </section>

      <section class="main-tab">
        <el-tabs v-model="active" @tab-click="handleTabClick">
          <el-tab-pane v-for="item in types" :key="item.id" :label="item.name" :name="item.id" />
        </el-tabs>
      </section>

      <section class="main-table">
        <v-table v-if="active == '1'" :list="list" tag="grantdetailuse" :list-loading="loading" :filter-column="filtercolumn" @handleSelectionChange="handleSelectionChange" />
        <v-table v-if="active == '2'" :list="list" tag="grantdetailorder" :list-loading="loading" :filter-column="filtercolumn" @handleSelectionChange="handleSelectionChange" />
        <pagination v-if="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </section>
    </section>
  </section>
</template>
<script>
import elDragDialog from '@/directive/el-drag-dialog'
import permission from '@/directive/permission/index.js'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import CryptoJS from '@/utils/aesJiaJM'
import { getToken } from '@/utils/auth'
import { queryPage, getDictByCode } from './../api/record'

export default {
  name: 'GrantDetail',
  components: { Pagination, VTable },
  directive: { permission, elDragDialog },
  data() {
    return {
      state: 'create',
      loading: false,
      search: {
        userRealname: '',
        phoneNumber: ''
      },
      list: [],
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      selection: [],
      dict: {
        numberPOOLSTATUS: [], //
        ZPID: []
      },
      t: '',
      id: '',
      showupload: false,
      showuser: false,
      types: [
        {
          name: '使用明细',
          id: '1'
        },
        {
          name: '订单明细',
          id: '2'
        }
      ],
      active: '1'
    }
  },
  created() {
    this.state = this.$route.query.type === 'c' ? 'create' : 'update'
    for (const key in this.dict) {
      this.getDict(key)
    }
    this.t = this.$route.query.t
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getList()
    } else {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.$router.back()
        }
      })
    }
  },
  methods: {
    filtercolumn(scope, item) {
      if (item === 'userState') {
        return scope.row[item] || '空'
      } else if (item === 'isDirectional') {
        return scope.row[item] === 'ZPID02' ? '否' : '是'
      } else {
        return scope.row[item]
      }
    },

    /**
     * 获取字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code).then(res => {
          this.dict[code] = (res.data.data && res.data.data.length > 0) ? res.data.data[0].list : []
        }).catch(error => {
          reject(error)
        })
      })
    },

    /**
     * 选项卡切换
     */
    handleTabClick(tab, event) {
      this.listQuery.page = 1
      this.getList()
    },

    handleExport() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = '/API-ENTERPRISE-BACKEND/backend/giveInfoDetail/giveInfoDetailExport?ttsxyhcs=' + getToken() + '&giveId=' + this.id
      link.setAttribute('download', '明细.xls')
      link.setAttribute('target', '_blank')
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      loading.close()
    },

    handleCancel() {
      this.$router.push({ 
				path:this.$filterPath("index"), query: { t: this.t } })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleReset() {
      this.listQuery.page = 1
      for (const i in this.search) {
        this.search['' + i + ''] = ''
      }
      this.getList()
    },

    handleSelectionChange(arr) {
      this.selection = arr
    },

    getList(obj) {
      this.loading = true
      var query = {
        giveId: this.id,
        queryFlag: 1,
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        type: this.active,
        userRealname: this.search.userRealname
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(({ data }) => {
          const list = data.data.rows
          if (list && list.length > 0) {
            this.list = list.map(a => {
              return {
                ...a,
                number: a.giveNumber,
                JPhoneNumber: CryptoJS.$aesJieM(a.phoneNumber),
                edit: false
              }
            })
          } else {
            this.list = []
          }
          this.total = data.data.total
          this.loading = false
        }).catch(error => {
          reject(error)
          this.loading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';
.grant-detail {
  min-height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden;

  .detail-main {
    position: absolute;
    top: 10px;
    left: 0;
    right: 0;
    bottom: 10px;
    box-sizing: border-box;
    padding: 0 10px;
    overflow: auto;
    &::-webkit-scrollbar {
      width: 8px; /*对垂直流动条有效*/
      height: 1px; /*对水平流动条有效*/
    }
    /** 滚动条轨道*/
    &::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: white;
      border-radius: 3px;
    }
    /** 滚动条里面小方块*/
    &::-webkit-scrollbar-thumb {
      height: 32px;
      border-radius: 5px;
      box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
      background-color: #eaeaea;
    }

    .main-search {
      background-color: #fff;
      padding: 0 10px;
      .svg-container {
        color: #bfced6;
        display: inline-block;
      }
      .search-input {
        width: 60%;
        position: relative;
        &:after {
          content: '';
          position: absolute;
          left: -25px;
          right: 0;
          bottom: 8px;
          height: 1px;
          background: rgba(226, 227, 232, 1);
        }
        input {
          outline: none;
          background: transparent;
          border: none;
          outline: medium;
          padding: 0;
          &:focus {
            outline: none;
            background-color: transparent;
          }
        }
      }
      .search-btn {
        display: inline-block;
        float: right;
        .el-button.is-round {
          padding: 10px 30px;
          margin-right: 34px;
        }
        .search-more {
          font-size: 14px;
          font-weight: 500;
          @include font-color($mainColor);
          cursor: pointer;
          margin-left: 10px;
        }
      }

      &.merge {
        height: 50px;
        line-height: 50px;
      }
    }

    .main-buttons {
      display: flex;
      line-height: 36px;
      margin-top: 10px;
      .main-buttons-title {
        flex: 1;
        height: 36px;
        line-height: 36px;
        font-size: 16px;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
      }
    }

    .main-tab {
      width: 100%;
      margin-top: 10px;
      padding: 0 10px;
      background-color: #fff;
      display: flex;
      border-radius: 4px;
      .el-tabs__nav-wrap::after {
        display: none;
      }
      .el-tabs__header {
        margin: 0;
      }
    }

    .main-table {
      .table-search {
        margin-bottom: 10px;
      }
      .edit-input {
        padding-right: 40px;
      }
      .cancel-btn {
        position: absolute;
        right: 15px;
        top: 10px;
      }
    }
  }
}
</style>
